# -*- coding: utf-8 -*-
#
# Copyright (c) 2007 Benoit Chesneau <benoitc@metavers.net>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import get_object_or_404
from django.shortcuts import render_to_response
from django.core.paginator import ObjectPaginator, InvalidPage
from django.template import loader, RequestContext
from django.conf import settings
from django.core.urlresolvers import reverse
from django.contrib.contenttypes.models import ContentType

from django.contrib.auth.decorators import login_required

#from django.utils.http import urlquote_plus, urlquote

from django.core.exceptions import ObjectDoesNotExist

from forms import CommentForm, CommentFormAuth
from models import Comment

#------------------------------------------------------------------------------
@login_required 
def delete_comment(request, object_id, comment_id):
    comment = Comment.objects.get(pk=comment_id)
    comment.delete()
    
    path = request.META['HTTP_REFERER']
    return HttpResponseRedirect("%s#comments" % path)

#------------------------------------------------------------------------------
def add_comment(request):
    """Add comment"""
    
    if request.POST:
        if request.user.is_authenticated():
            form = CommentFormAuth(request.POST)
        else:
            form = CommentForm(request.POST)
        
        obj_id = form.data['object_id']
        content_type=ContentType.objects.get(id=form.data['content_type'])
        
        if form.is_valid():
            if request.user.is_authenticated():
                user_name = request.user
            else:
                user_name = form.clean_data['user_name']
            
            if content_type:
                params = {
                    'user_name': user_name,
                    'comment':form.clean_data['comment'],
                    'content_type':content_type,
                    'object_id':form.clean_data['object_id'],
                }
                c = Comment(**params)
                c.save()
                
                try:
                    obj = content_type.get_object_for_this_type(pk = form.clean_data['object_id'])
                except ObjectDoesNotExist:
                    pass

        path = request.META['HTTP_REFERER']
        return HttpResponseRedirect("%s#comments" % path)
    else:
        raise Http404()

#------------------------------------------------------------------------------ 
def ajax_comment_form(request, content_type_id, object_id):
    """New comment form with AJAX behavior"""
    
    if request.user.is_authenticated():
        form = CommentFormAuth(initial={'content_type': content_type_id, 'object_id': object_id,})
    else:
        form = CommentForm(initial={'content_type': content_type_id, 'object_id': object_id,})
    
    content = {
               'form_comment': form, 
               'form_comment_action': '/comments/add/'
               }
    
    return render_to_response("comments/ajax_form.html", content, context_instance=RequestContext(request)) 

#------------------------------------------------------------------------------ 
